package com.hngy.xpq.android_practice.ui.notifications;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.hngy.xpq.android_practice.R;
import com.hngy.xpq.android_practice.bean.Wallet;
import com.hngy.xpq.android_practice.database.SQLiteWallet;
import com.hngy.xpq.android_practice.http.TcpClient;
import com.hngy.xpq.android_practice.utils.WalletOrder;

public class WalletRecharge extends AppCompatActivity {

    EditText money;
    Button bin;
    SQLiteWallet sqLiteWallet;
    SharedPreferences main;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wallet_recharge);

        money = findViewById(R.id.wallet_sql_money);
        bin = findViewById(R.id.bin);
        //钱包数据库的初始化
        sqLiteWallet = new SQLiteWallet(this);
        //获得充值用户
        main = super.getSharedPreferences("main",MODE_PRIVATE);

        money.addTextChangedListener(new TextWatcher() {    //监听EditText , 控制金额在两位小数内
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String temp = s.toString();
                int pos = temp.indexOf(".");
                if (pos <= 0)
                    return;
                if (temp.length() - pos - 1 > 2)
                {
                    s.delete(pos + 3 , pos + 4 );
                }
            }
        });
        bin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Wallet w = new Wallet();
                w.setName(main.getString("name",""));
                w.setSingle_money(money.getText().toString());
                w.setTime(WalletOrder.getTimeDate());
                w.setDes("密码充值");
                String information = "CZ001//" + w.getName() + "//" + w.getSingle_money() + "//" + "手机密码充值";
                String a = new TcpClient().Transits(information);
                System.out.println(a);
                if (a.equals("充值成功") && sqLiteWallet.recharge(w)) {
                    Intent intent = new Intent(WalletRecharge.this,WalletActivity.class);
                    startActivity(intent);
                    finish();
                }
            }
        });
    }

}